package com.yycx.module.bus.provider.handler.haiying.order.save;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.ConvertUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.hy.BusBankStatement;
import com.yycx.module.bus.client.entity.hy.BusHyorderReturn;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.client.enums.HySaleTypeEnum;
import com.yycx.module.bus.client.vo.OrderCustomReturn;
import com.yycx.module.bus.provider.handler.haiying.BusinessUtils;
import com.yycx.module.bus.provider.handler.haiying.HyOrderUtils;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.hy.BusBankStatementService;
import com.yycx.module.bus.provider.service.hy.BusHyorderReturnService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单保存扩展器
 *
 * @author zyf
 */
@Component
public class HyOrderSaveHandler implements SaveInterceptor<HyOrder> {

    @Resource
    private HyOrderService hyOrderService;

    @Resource
    private BaseUserService userService;


    @Resource
    private SysCompanyService companyService;

    @Resource
    private BusinessUtils businessUtils;

    @Resource
    private BusCustomService customService;

    @Resource
    private HyOrderUtils hyOrderUtils;

    @Override
    @DSTransactional
    public void prepare(CriteriaSave cs, EntityMap entityMap, HyOrder hyOrder) {

        Integer historyOrder = cs.getInt("historyOrder", 0);
        Long companyId = OpenHelper.getCompanyId();
        Long userId = OpenHelper.getUserId();
        String customCode = hyOrder.getCustomCode();
        BaseUser user = userService.getById(userId);
        SysCompany company = companyService.getById(companyId);
        //获取订单号
        String orderNo = businessUtils.getOrderNo(company, user);
        ApiAssert.isNotEmpty("订单号获取失败", orderNo);
        hyOrder.setOrderId(orderNo);
        //获取订单号
        hyOrder.setOrderNo(orderNo);
        hyOrder.setCompanyId(companyId);
        hyOrder.setHostName(user.getHostName());
        //查询客户资料
        BusCustom custom = customService.selectByCustomCode(customCode, companyId);
        if (FlymeUtils.isEmpty(custom)) {
            custom = customService.syncBusCustom(company.getCompanyErpInstance(), customCode, companyId);
            ApiAssert.isNotEmpty("客户资料未同步", custom);
        }
        hyOrder.setCustomId(custom.getCustomId());
        //销售订单
        hyOrder.setOrderType(HySaleTypeEnum.CHA.getCode());
        hyOrder.setNickName(user.getNickName());
        //未提交
        hyOrder.setOrderState(CommonConstants.INT_0);
        //未提单
        hyOrder.setSendState(CommonConstants.INT_0);
        //同步状态
        hyOrder.setSyncTag(0);
        hyOrder.setPrintTag(0);
        hyOrder.setOutPrintTag(0);
        hyOrder.setOutPrintTag2(0);
        hyOrder.setRebateState(0);
        hyOrder.setProLevel(0);
        //@zyf@制单日期
        //hyOrder.setCreateDate(DateUtil.date());
        //结算系统录入(1:海鹰导入,2:结算系统录入)
        hyOrder.setSourceType(2);
        //核算状态
        hyOrder.setCalculateState(0);
        //开票员
        hyOrder.setUserId(userId);
        //订单前置校验
        BusSaleman saleMan = hyOrderService.checkOrder(hyOrder, true, historyOrder);
        //设置订单信息
        hyOrderService.setHyOrderInfo(hyOrder, saleMan);
        //查询订单相关提成人员
        hyOrderUtils.setSaleManInfo(hyOrder, saleMan);
        String drugList = cs.getParams("drugList");
        //保存订单明细
        hyOrderService.saveOrderDetails(hyOrder, drugList, true);
    }

    @Override
    @DSTransactional
    public void complete(CriteriaSave cs, EntityMap params, HyOrder hyOrder) {

    }



}
